/**
 * Calculate the length of time between two time
 */
package com.pxingg.datetime;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Calculate the length of time between two time
 * 
 * @author Administrator
 * 
 */
public class Timer
{
    /**
     * The Millisecond for timer start, base on 1970-01-01 00:00:00
     */
    long start;
    /**
     * The Millisecond for timer end, , base on 1970-01-01 00:00:00
     */
    long end;

    /**
     * The name of the timer
     */
    String name = "dummy";

    /**
     * The invalid value of start or end.
     */
    final static long INVALID_TIME_STAMP = 0;

    /**
     * The Default construction function
     */
    public Timer()
    {
        start = end = Timer.INVALID_TIME_STAMP;
    }

    /**
     * The Default construction function
     */
    public Timer(final String name)
    {
        start = end = Timer.INVALID_TIME_STAMP;
        this.name = name;
    }

    /**
     * @return the end
     */
    public long getEnd()
    {
        return end;
    }

    /**
     * Get the interval between the timer's start and stop
     * 
     * @return return the time interval between the timer's start and stop <br>
     *         if not start return 0 <br>
     *         if not end return the interval betwen start and current time<br>
     * 
     */
    public long getInterval()
    {
        if (start == Timer.INVALID_TIME_STAMP)
        {
            return 0;
        }

        if (end == Timer.INVALID_TIME_STAMP)
        {
            return System.currentTimeMillis() - start;
        }

        return end - start;
    }

    /**
     * @return the name
     */
    public String getName()
    {
        return name;
    }

    /**
     * @return the start
     */
    public long getStart()
    {
        return start;
    }

    /**
     * 把毫秒为单位的计时，转换成yyyy-MM-dd hh:mm:ss.SSS的格式
     * 
     * @param time
     *            毫秒的计时
     * @return yyyy-MM-dd hh:mm:ss.SSS的格式的时间字符串
     */
    protected String getTimebyMillisecond(final long time)
    {
        final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");

        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);

        return formatter.format(calendar.getTime());
    }

    /**
     * 计时器是否启动，并且正在计时，没有停止
     * 
     * @return true: 计时器已经启动，并且正在计时，没有停止 <br>
     *         false: 计时器已经停止活着没有启动
     * @return
     */
    public boolean isStarted()
    {
        return start != Timer.INVALID_TIME_STAMP && end == Timer.INVALID_TIME_STAMP;
    }

    /**
     * 计时器是否停止
     * 
     * @return true: 计时器已经停止 <br>
     *         false: 计时器还没有停止
     */
    public boolean isStoped()
    {
        return end != Timer.INVALID_TIME_STAMP;
    }

    /**
     * To restart the timer
     */
    public void restart()
    {
        start = System.currentTimeMillis();
        end = Timer.INVALID_TIME_STAMP;
    }

    /**
     * @param end
     *            the end to set
     */
    public void setEnd(final long end)
    {
        this.end = end;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(final String name)
    {
        this.name = name;
    }

    /**
     * @param start
     *            the start to set
     */
    public void setStart(final long start)
    {
        this.start = start;
    }

    /**
     * To start the timer
     */
    public void start()
    {
        start = System.currentTimeMillis();
        end = Timer.INVALID_TIME_STAMP;
    }

    /**
     * To stop the timer
     */
    public void stop()
    {
        end = System.currentTimeMillis();
    }

    @Override
    public String toString()
    {
        final long currentInterval = getInterval();

        if (start == Timer.INVALID_TIME_STAMP)
        {
            return String.format("Timer[%s] is not start yet", name);
        }
        else if (end == Timer.INVALID_TIME_STAMP)
        {
            return String.format("Timer[%s] from %s, but not stopped, to current interval is %d.%03d seconds", name, getTimebyMillisecond(start), currentInterval / 1000, currentInterval % 1000);
        }
        else
        {
            return String.format("Timer[%s] from %s to %s, interval = %d.%03d seconds", name, getTimebyMillisecond(start), getTimebyMillisecond(end), currentInterval / 1000, currentInterval % 1000);
        }
    }

}
